from manim import *
import math
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [34, 20, 0],
            [36,11,0],
            [45,9,0]
        ]
        self.set_points_smoothly(points)



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
         #镜头跟进效果 
        self.camera.frame.save_state()
         
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
       
        

        #主题展开：
        #1文本
        text5 = Tex(
                    r"$\text{反常积分}$",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-5,0])
        #2框框标
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-35,-5,0])
        #3箭头
        arrow1 = Arrow(start=[-24,-2,0],end=[-20,2.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        
        arrow3 = Arrow(start=[-24,-7,0],end=[-20,-12.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        #4文本
        text6 = Tex(
                    r"\text{判敛散性}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-12,2.5,0])
        
        text8 = Tex(
                    r"\text{计算}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-12.5,0])
        #5箭头
        arrow4 = Arrow(start=[-5,4,0],end=[0,10,0], color=RED
                       , stroke_width=20, tip_length=1)
        
        
        arrow5 = Arrow(start=[-5,2,0],end=[0,-2,0], color=RED
                       , stroke_width=20, tip_length=1)
        





        
        arrow8 = Arrow(start=[-10,-11.5,0],end=[-4,-8.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow9 = Arrow(start=[-10,-13.5,0],end=[-4,-17.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        text9 = Tex(
                    r"\text{P积分}}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([6,10,0])
        text10 = Tex(
                    r"\text{非P积分}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([6,-2,0])
        
        text13 = Tex(
                    r"\text{不定积分+求极限}",color=GREEN_C 
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-8.5,0])
        text130 = Tex(
                    r"\text{换元+区间在现}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-17.5,0])
        self.play(Write(text5),Create(rectangle1))
        self.play(Create(arrow1),Create(arrow3))
        self.play(Create(text6),Create(text8))
        self.play(Create(arrow4),Create(arrow5),Create(arrow8),Create(arrow9))
        self.play(Create(text9),Create(text10),Create(text13),Create(text130))
        #
        rectangle100=Rectangle(color=BLUE,fill_opacity=0.0,width=12,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle100.move_to([6,10,0])
        self.play(Create(rectangle100))
        self.play(FadeOut(rectangle100))
         #
        rectangle101=Rectangle(color=BLUE,fill_opacity=0.0,width=12,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle101.move_to([6,-2,0])
        self.play(Create(rectangle101))
        self.play(FadeOut(rectangle101))
         #转化
        arrow9 = Arrow(start=[4,0,0],end=[7,8,0], color=BLUE
                       , stroke_width=20, tip_length=1)
        text14 = Tex(
                    r"\text{转化}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([12,5,0])


        arrow10 = Arrow(start=[12,10,0],end=[22,10,0], color=BLUE
                       , stroke_width=20, tip_length=1)
        text15 = Tex(
                    r"\text{看图判断趋势}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17,15,0])
        
        arrow11 = Arrow(start=[14,-2,0],end=[25,5,0], color=BLUE
                       , stroke_width=20, tip_length=1)
        text16 = Tex(
                    r"\text{看图判断趋势}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,0,0])
        
        '''
         # 创建坐标系，具体设置参数参考课程--->5.3-数轴&坐标轴
        axes = Axes(
            x_range=[-5, 5, 0.3],
            y_range=[-10, 10, 1],
            axis_config={
                "color": BLUE, 
                "include_numbers": True, 
                "label_direction": UP,
                "font_size": 18,
                "stroke_width": 30
            },
        )
        axes.move_to([35,10,0])
         # 定义要绘制的函数
        def my_function(x):
            return 1 / x

        # 因为e^x/x这个函数不能让x=0，所以要分为两段去画
        # plot方法第一个参数就是自定义的函数对象，后面不要加括号，错误示例：my_function()、my_function(x)
        # x_range参数设置x的定义域
        #graph1 = axes.plot(my_function, x_range=[-5, -0.1], color=YELLOW)
        graph2 = axes.plot(my_function, x_range=[0.1, 5], color=RED)

        # 添加标签
        labels = axes.get_axis_labels(x_label="x", y_label="1 / x")

       '''


         
        # 创建坐标系
        axes = Axes(
            x_range=[-5, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-5, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes.move_to([35, 10, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

        # 添加标题
        title = Text("f(x) = 1/x", color=GREEN, font_size=240).next_to(axes, UP, buff=1)

       


        self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        curve = CustomCurve(color=PURE_RED, stroke_width=20)
        

        self.play(Create(axes), Write(title), Create(curve))
        self.play(Create(arrow9),Create(text14))
        self.play(Create(arrow11),Create(text16))
        self.wait()
        self.play(self.camera.frame.animate.scale(0.5).move_to([35,10,0]))